package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.system.pojo.SysRole;
import com.aisile.crowd_funding.system.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Scanner;

/**
 * @标题 RoleController
 * @作者 Mr.chen
 * @创建日期 2018/12/10 14:26
 * @版本号 1.0
 **/
@Controller
@RequestMapping("sys/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 跳转角色页面
     * @return
     */
    @RequestMapping
    public String index(){

        return "system/role/role";
    }

    /**
     * 查询角色列表
     * @return
     */
    @RequestMapping(value="list")
    @ResponseBody
    public List<SysRole> list(){
        return roleService.selectByExample(null);
    }

    /**
     * 根据ID删除角色 及 关联数据
     * @param roleId
     * @return
     */
    @RequestMapping(value = "del")
    @RequiresPermissions("sys:role:remove")
    @ResponseBody
    public R del(Long roleId){
        int i = roleService.deleteByPrimaryKey(roleId);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("batchDel")
    @RequiresPermissions("sys:role:batchRemove")
    @ResponseBody
    public R batchDel(Long[] ids){
        if(ids==null||ids.length<=0){
            return R.error("请选择要删除的数据");
        }
        roleService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("add")
    @RequiresPermissions("sys:role:add")
    public String add(){
        return "system/role/add";
    }
    @RequestMapping("save")
    @RequiresPermissions("sys:role:add")
    @ResponseBody
    public R save(SysRole sysRole){
        int insert = roleService.insert(sysRole);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 跳转角色编辑页面
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("edit/{roleId}")
    @RequiresPermissions("sys:role:edit")
    public String edit(@PathVariable Long roleId,Model model){
        SysRole sysRole = roleService.selectByPrimaryKey(roleId);
        model.addAttribute("role",sysRole);
        return "system/role/edit";
    }


    @RequestMapping("update")
    @RequiresPermissions("sys:role:edit")
    @ResponseBody
    public R update(SysRole sysRole){
        int insert = roleService.updateByPrimaryKey(sysRole);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }
}
